package com.dreamchaser.familydevice.config.device;


import com.dreamchaser.familydevice.config.SpringContextHolder;
import com.dreamchaser.familydevice.constant.DeviceCache;
import com.dreamchaser.familydevice.service.IDeviceService;
import com.dreamchaser.familydevice.util.device.ByteHexUtil;
import com.dreamchaser.familydevice.util.device.DeviceDataUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

/***
 *
 *
 * @Author: SJie
 * Date: 2019/12/19 下午5:44
 * Description:
 *
 */
@Slf4j
public class DeviceDataHandler extends ChannelInboundHandlerAdapter {
    public DeviceDataHandler() {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Server1:" + "channelInactive");

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        System.out.println(msg.toString());
//        return;
        byte[] data = (byte[]) msg;
        if (ByteHexUtil.byteToHex(data[0]) == 0x00) {
            log.info( ByteHexUtil.byteToHex(data[1])+"号设备上线");
            int deviceCode = ByteHexUtil.byteToHex(data[1]);
            DeviceDataUtil.setDeviceKey(ctx.channel(), deviceCode);
            IDeviceService deviceService = SpringContextHolder.getBean(IDeviceService.class);
            if (!deviceService.validDevice(deviceCode)) {
                log.warn("未知设备");
                return;
            }
            // 发送初始化设备状态
            log.warn(deviceCode + "：发送设备初始化状态");
            deviceService.sendInitStatus(deviceCode, ctx.channel());

        } else if (data.length == 1) {
            // 设备主动推送

            int device = DeviceDataUtil.getDeviceCode(ctx.channel());
            log.info(device + "号设备收到组件主动推送：" + ByteHexUtil.bytesToHexStr(data[0]));
            IDeviceService deviceService = SpringContextHolder.getBean(IDeviceService.class);
            deviceService.refreshDeviceStatus(DeviceDataUtil.getDeviceExpressionBydeviceCode(device), data[0], ctx.channel());
        } else if (data.length == 2) {
            // 动作成功
            if (data[1] == 0x00) {

                int device = DeviceDataUtil.getDeviceCode(ctx.channel());
                log.info(device + "号设备动作响应成功，动作：" + ByteHexUtil.hexToHexStr(data[0]));
                IDeviceService deviceService = SpringContextHolder.getBean(IDeviceService.class);
                StringRedisTemplate stringRedisTemplate = SpringContextHolder.getBean(StringRedisTemplate.class);
                byte[] d = {data[1], data[0]};
                // 移除设备延时相关任务
                DeviceDataUtil.removeTask("delay" + ByteHexUtil.hexToHexStr(device),stringRedisTemplate);
                DeviceDataUtil.removeTask("init_delay" + ByteHexUtil.hexToHexStr(device),stringRedisTemplate);
                deviceService.setDeviceStatus(DeviceDataUtil.getDeviceExpressionBydeviceCode(device), d);
            } else if (data[1] == 0x01) {
                int device = DeviceDataUtil.getDeviceCode(ctx.channel());
                log.info(device + "号设备动作响应失败，动作：" + ByteHexUtil.hexToHexStr(data[0]));
            }


        }


    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

}
